#include "stdafx.h"
#include "RTTimer.h"

bool		RTTimer::m_bCheckQPF	= false;
bool		RTTimer::m_bCanUseQPF	= false;
ULONGLONG	RTTimer::m_freq			= 1;

RTTimer::RTTimer()
{
	Reset();
}

RTTimer::~RTTimer()
{

}

void	RTTimer::Reset()
{
	if( !m_bCheckQPF )
	{
		LARGE_INTEGER lpPC;
		m_bCanUseQPF = (bool)(QueryPerformanceFrequency( &lpPC ) != 0);
		if( m_bCanUseQPF )
			m_freq = lpPC.QuadPart;
		m_bCheckQPF = true;
	}

	m_currentTime	= 0;
	m_lastTime		= 0;
	m_elapsedTime	= 0;
}

const double	RTTimer::Update()
{
	if( m_bCanUseQPF )
	{
		LARGE_INTEGER lpPC;
		QueryPerformanceCounter( &lpPC );
		m_currentTime = lpPC.QuadPart;

		m_elapsedTime = (double)(m_currentTime - m_lastTime) / (double)m_freq;

		m_lastTime = m_currentTime;
	}
	else
	{
		m_currentTime = timeGetTime() * 1000;

		m_elapsedTime = (double)(m_currentTime - m_lastTime) / (double)1000000.0;
	}

	return m_elapsedTime;
}

const double	RTTimer::GetElapsedTime() const
{
	return m_elapsedTime;
}
